package mwa.cs.dao;

import java.util.Map;

import mwa.cs.database.Database;
import mwa.cs.model.SMUserRole;

public class SMUserRoleDAO {

private Database db;
	
	public SMUserRoleDAO(Database db) {
        this.db = db;
    }
	
	public Map<String, Object> getUserRoleAllForEdit(String paUserId) {
		
		String vaSql = "SELECT * FROM sm_user_role WHERE user_id = ?";
		Map<String, Object> result = db.querySingle(vaSql, paUserId);
		if (result != null) {
            return result;
        } else {
            return null;
        }
	
	}
	
	// เพิ่มข้อมูล SMUserRole
	public boolean insertUserRole(SMUserRole poNewUserRole) {
        String vaSql = "INSERT INTO sm_user_role ( "
      		  + "user_id, role_id, insert_user_id, insert_date, insert_time, update_user_id) "
      		  + "VALUES (?, ?, ?, ?, ?, ?)";
        boolean result = false;
        try {
        	db.update(vaSql,
        			poNewUserRole.getVnUserId(),
        			poNewUserRole.getVaRoleId(),
        			poNewUserRole.getVnInsertUserId(),
        			poNewUserRole.getVaInsertDate(),
        			poNewUserRole.getVaInsertTime(),
        			poNewUserRole.getVnUpdateUserId()
        			);
        	result = true;
        } catch (Exception e) {
        	result = false;
        }
        return result;
	}
	
	// แก้ไขข้อมูล UserLogin 
		public boolean updateUserRole(SMUserRole poNewUserRole) {
	        String vaSql = "UPDATE sm_user_role SET "
	      		  + "role_id = ?, update_user_id = ?, update_date = ?, update_time = ? "
	      		  + "WHERE user_id = ?";
	        boolean result = false;
	        try {
	        	db.update(vaSql,
	        			poNewUserRole.getVaRoleId(),
	        			poNewUserRole.getVnUpdateUserId(),
	        			poNewUserRole.getVaUpdateDate(),
	        			poNewUserRole.getVaUpdateTime(),
	        			poNewUserRole.getVnUserId()
	        			);
	        	result = true;
	        } catch (Exception e) {
	        	result = false;
	        }
	        return result;
		}
}
